home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6983 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.2 KB

  1. Path: in1.uu.net!csnews!mox!tchrist
  2. From: Tom Christiansen <tchrist@mox.perl.com>
  3. Newsgroups: comp.lang.misc,comp.lang.perl.misc,comp.lang.tcl,comp.lang.c,comp.lang.java
  4. Subject: Re: Readable Perl (was: Re: Relative Speed of Perl vs. Tcl vs. C)
  5. Date: 16 Feb 1996 22:48:23 GMT
  6. Organization: Perl Consulting and Training
  7. Message-ID: <4g31jn$q71@csnews.cs.colorado.edu>
  8. References: <4e3a2u$eoa@wcap.centerline.com> <JTV2J.96Feb12142743@mamba.cs.virginia.edu> <ukd97hwzkc.fsf_-_@linda.teleport.com> <4g23sv$dht@alpine.valleynet.com>
  9. Reply-To: tchrist@mox.perl.com (Tom Christiansen)
  10. NNTP-Posting-Host: perl.com
  11. Content-Type: text/plain; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. Originator: tchrist@mox
  14.  
  15.  [courtesy cc of this posting sent to cited author via email]
  16.  
  17. In comp.lang.perl.misc, jared@valleynet.com (Jared Still) writes:
  18. :merlyn@stonehenge.com (Randal L. Schwartz) wrote:
  19. :
  20. :>>>>>> "John" == John Viega <jtv2j@mamba.cs.virginia.edu> writes:
  21. :
  22. :>John> People bitch about the readability of Perl non-stop.  In fact, I have
  23. :
  24. :>OK, now which do you find most readable...
  25. :
  26. :>      $i = 1;
  27. :>      while ($i <= 10) {
  28. :>              print "I can count to $i\n";
  29. :>              $i++;
  30. :>      }
  31. :
  32. :.....
  33. :
  34. :>      foreach $i (1..10) {
  35. :>              print "I can count to $i\n";
  36. :>      }
  37. :
  38. :>Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
  39. :
  40. :None of the above.
  41. :
  42. :I like my curly braces to line up.
  43. :
  44. :       foreach $i (1..10) 
  45. :       {
  46. :               print "I can count to $i\n";
  47. :       }
  48.  
  49. CBD -- C brain damage.  While I understand why you are doing this, it
  50. doesn't make any sense in Perl, only in C.  You are working under the
  51. misconception that the "while (...)" etc is the "ok, let's is the begin
  52. while loop now" construct.  It's not.  That construct is "while (...) {".
  53. Note the mandatory brace.  In pseudo code, you'd have
  54.  
  55.     1 iterate across i from 1 to 10
  56.     2    print something
  57.     3 end i iteration
  58.  
  59. Which directly translates 
  60.  
  61.     1 foreach $i (1..10) {
  62.     2   print "I can count to $i\n";
  63.     3 }
  64.  
  65. Statements 1 and 3 are the begin and end.  The way you do it, you've got a
  66. dangling curly that takes up a line without doing anything, and confusing
  67. readers about what it's there for.   The close curly syntactially matches
  68. up with the if, while, for, or whatever, because the open curly is a part
  69. of them, not optional as it is in C.  To find where the block that
  70. line 3 closes starts, just find the first non white space at that 
  71. same column.
  72.  
  73. That's why Larry writes in perlstyle(1)
  74.  
  75.     Regarding aesthetics of code lay out, about the only thing Larry cares
  76.     strongly about is that the closing curly brace of a multi-line BLOCK
  77.     should line up with the keyword that started the construct. Beyond
  78.     that, he has other preferences that aren't so strong:
  79.  
  80.     *   4-column indent.
  81.     ^^^^^^^^
  82.  
  83.     *   Opening curly on same line as keyword, if possible, otherwise line up.
  84.                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  85.                                                ^^^^^^^^^^^
  86.  
  87.     *   Space before the opening curly of a multiline BLOCK.
  88.  
  89.     *   One-line BLOCK may be put on one line, including curlies.
  90.  
  91.     ......
  92.  
  93.  
  94. The "when it's not possible case" is demonstrated here:
  95.  
  96.     while (<>) { # YANETUT
  97.         s/^(([A-Z].*?)(:.*?)*?)(:\s*)?\n// 
  98.             and ($3 || $4 ? $card : $sect) = $1;
  99.         next if /^\s*[=-]/;
  100.         for (split /\n(?=  \S|\+)/) {
  101.             s/\s+/ /g; s/^ //;
  102.             $new = s/^\+\s+// ? '+' : '';
  103.             next if $opt_n and !$new;
  104.             s/([a-z]\.) ([A-Z])/$1\n$2/g if $opt_s;
  105.             if ( ($opt_e ? $card : $_) =~ /$query/o || 
  106.                     (!$opt_e && $opt_h && $card =~ /$query/o)) 
  107. >>>>        {
  108.                 s/\n/ /g if $opt_s;
  109.                 $matches++;
  110.                 write;
  111.                 ($psect, $pcard) = ($sect, $card);
  112.             }
  113.         } 
  114.     } 
  115.  
  116. You only do that when you've got continuation lines.
  117.  
  118.     if (    asdfasdfsda
  119.      && asfljkasdjkasdf1
  120.      && asfljkasdjkasdf2
  121.      && asfljkasdjkasdf3
  122.      && asfljkasdjkasdf4
  123.        )
  124.     {
  125.     zmcbzxb
  126.     } 
  127.  
  128. But normally just 
  129.  
  130.     if (asdfasdfsda) {
  131.     zmcbzxb
  132.     }
  133.  
  134. --tom
  135. -- 
  136. Tom Christiansen      Perl Consultant, Gamer, Hiker      tchrist@mox.perl.com
  137.  
  138.     X-Windows: Don't get frustrated without it.
  139.     --Jamie Zawinski
  140.